import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import Login from '_views/Login'
import { isLogin } from '_utils/getlogin'
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('_views/Home'),
    mete: {
      isLogin: true
    },
    children: [
      {
        path: '/home',
        redirect: '/home/dashboad'
      },
      {
        path: '/home/dashboad',
        name: 'Dashboad',
        meta: {
          name: '数据可视化',
          isLogin: true,
          isNav: true,
          roles: ['admin', 'superadmin']
        },
        component: () => import('_views/Dashboad')
      },
      {
        path: '/home/itemlist',
        name: 'Itemlist',
        meta: {
          name: '商品列表',
          isLogin: true,
          isNav: true,
          roles: ['admin', 'superadmin']
        },
        component: () => import('_views/Itemlist')
      },
      {
        path: '/home/itemlist/add',
        name: 'ItemAdd',
        meta: {
          name: '商品增加',
          isLogin: true,
          isNav: false,
          roles: ['admin', 'superadmin']
        },
        component: () => import('_views/Itemadd')
      },
      {
        path: '/home/itemlist/write/:id',
        name: 'Itemwrite',
        meta: {
          name: '商品编辑',
          isLogin: true,
          isNav: false,
          roles: ['admin', 'superadmin']
        },
        component: () => import('_views/Itemwrite')
      },
      {
        path: '/home/itemupdate',
        name: 'Itemupdate',
        meta: {
          name: '商品管理',
          isLogin: true,
          isNav: true,
          roles: ['admin', 'superadmin']
        },
        component: () => import('_views/Itemupdate')
      },
      {
        path: '/home/message',
        name: 'Message',
        meta: {
          name: '消息中心',
          isLogin: true,
          isNav: true,
          roles: ['admin', 'superadmin']
        },
        component: () => import('_views/Message')
      },
      {
        path: '/home/other',
        name: 'Other',
        meta: {
          name: '其他',
          isLogin: true,
          isNav: true,
          roles: ['admin', 'superadmin']
        },
        component: () => import('_views/Other')
      },
      {
        path: '/home/admin',
        name: 'Admin',
        meta: {
          name: '管理员',
          isLogin: true,
          isNav: true,
          roles: ['admin']
        },
        component: () => import('_views/Admin')
      },
      {
        path: '/home/superadmin',
        name: 'Superadmin',
        meta: {
          name: '超级管理员',
          isLogin: true,
          isNav: true,
          roles: ['superadmin']
        },
        component: () => import('_views/Superadmin')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.isLogin) {
    if (isLogin()) {
      next()
    } else {
      router.push({
        path: '/login'
      })
    }
  } else {
    next()
  }
})
export default router
